package com.thinkgem.jeesite.modules.data.rep.web;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.data.rep.entity.TfEcReportConfigOut;
import com.thinkgem.jeesite.modules.data.rep.service.ReportConfService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 报表配置输出参数controller
 *
 * @author Havel
 */
@Controller
@RequestMapping(value = "${adminPath}/report/configOut")
public class ConfigOutController extends BaseController {
    @Autowired
    ReportConfService reportConfService;

    @RequiresPermissions("report:configOut:view")
    @RequestMapping(value = {"list", ""})
    public String list(@RequestParam long reportId, TfEcReportConfigOut reportConfigOut, HttpServletRequest request, HttpServletResponse response, Model model) {
        reportConfigOut.setReportId(reportId);
        Page<TfEcReportConfigOut> page = reportConfigOut.getPage();
        page.setPageNo(getPageNo(request, response));
        page.setPageSize(getPageSize(request, response, 10));
        page = reportConfService.findPage(page, reportConfigOut);

        model.addAttribute("page", page);
        model.addAttribute("reportId", reportId);
        return "modules/data/rep/reportConfigOutList";
    }

    @RequiresPermissions("report:configOut:edit")
    @RequestMapping(value = "form")
    public String form(String configOutId, String reportId, Model model) {
        TfEcReportConfigOut reportConfigOut = new TfEcReportConfigOut();
        if(!StringUtils.isEmpty(configOutId)){
            reportConfigOut = reportConfService.getConfigOutById(Long.parseLong(configOutId));
        }
        model.addAttribute("reportConfigOut", reportConfigOut);
        model.addAttribute("reportId", reportId);
        return "modules/data/rep/reportConfigOutForm";
    }

    @RequiresPermissions("report:configOut:edit")
    @RequestMapping(value = "save")
    public String save(TfEcReportConfigOut reportConfigOut, @RequestParam long reportId, HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) {
        if (beanValidator(model, reportConfigOut)) {
            reportConfigOut.setReportId(reportId);
            reportConfigOut.setUpdateStaffId(UserUtils.getUser().getPhone());
            reportConfService.saveAndUpdate(reportConfigOut);
            this.addMessage(redirectAttributes, "保存" + StringUtils.abbr(reportConfigOut.getItemName(), 50) + "成功");
        } else {
            return form(reportConfigOut.getOutResultId()+"", reportId+"", model);
        }
        return "redirect:" + adminPath + "/report/configOut?reportId=" + reportId;
    }

    @RequiresPermissions("report:configOut:edit")
    @RequestMapping(value = "delete")
    public String delete(@RequestParam(required = false) long configOutId, @RequestParam long reportId, Model model, RedirectAttributes redirectAttributes) {
        TfEcReportConfigOut reportConfigOut = new TfEcReportConfigOut();
        reportConfigOut.setOutResultId(configOutId);
        reportConfigOut.setUpdateStaffId(UserUtils.getUser().getPhone());
        reportConfService.delete(reportConfigOut);
        this.addMessage(redirectAttributes, "删除" + StringUtils.abbr(reportConfigOut.getItemName(), 50) + "成功");
        return "redirect:" + adminPath + "/report/configOut?reportId=" + reportId;
    }
}
